package cn.edu.lzu.service.impl;

import cn.edu.lzu.common.assist.DataPage;
import cn.edu.lzu.common.assist.ResponseResult;
import cn.edu.lzu.dao.ExaminePackageMapper;
import cn.edu.lzu.dao.ItemPackageMapper;
import cn.edu.lzu.entity.ExaminePackage;
import cn.edu.lzu.service.ExaminePackageService;
import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @PackageName: cn.edu.lzu.service.impl
 * @ClassName: ExaminePackageServiceImpl
 * @Description: TODO
 * @Author: 张琦[QQ:3098086691]
 * @Date: 2019/12/10 15:04
 * @Version: 1.0
 */
@Service
@Transactional
public class ExaminePackageServiceImpl implements ExaminePackageService {
    @Resource
    private ExaminePackageMapper examinePackageMapper;
    @Resource
    private ItemPackageMapper itemPackageMapper;

    @Override
    public DataPage selectPageExaminePackageByUnitId(long unitId, int currentPageSize, int pageSize) {
        int startIndex = (currentPageSize - 1) * pageSize;
        List<ExaminePackage> examinePackageList = examinePackageMapper.selPageExaminePackageByUnitId(unitId, startIndex, pageSize);
        int totalCount = examinePackageMapper.selPackageTotalCountByUnitIdNotDeleted(unitId);
        DataPage packagePage = new DataPage();
        if (totalCount >= 0 && examinePackageList.size() >= 0) {
            packagePage.setMsg("成功");
            packagePage.setCode(0);
            packagePage.setTotalCount(totalCount);
            packagePage.setData(examinePackageList);
        } else {
            packagePage.setMsg("失败");
            packagePage.setCode(1);
            packagePage.setData(null);
            packagePage.setTotalCount(0);
        }
        return packagePage;
    }

    @Override
    public ResponseResult deleteExaminePackageByUnitId(int examinePackageId, long unitId) {
        //删除套餐examinePackage记录
        int result = examinePackageMapper.updDeleteExaminePackageById(examinePackageId);
        //删除套餐项目关联记录
        int result1 = itemPackageMapper.updDeleteByPackageId(examinePackageId, unitId);
        ResponseResult responseResult = new ResponseResult();
        if (result == 1 && result1 >= 0) {
            responseResult.setCode(1);
            responseResult.setMsg("套餐删除成功");
        } else {
            responseResult.setCode(0);
            responseResult.setMsg("套餐删除失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult batchDeleteExaminePackagesByIds(String examinePackageIds, long unitId) {
        int result = examinePackageMapper.updBatchDeleteExaminePackagesByIds(examinePackageIds);
        int result1 = itemPackageMapper.updBatchDeleteByPackageId(examinePackageIds, unitId);
        ResponseResult responseResult = new ResponseResult();
        if (examinePackageIds.split(",").length == result && result1 >= 0) {
            responseResult.setCode(1);
            responseResult.setMsg("套餐批量删除成功！");
        } else {
            responseResult.setCode(0);
            responseResult.setMsg("套餐批量删除失败！");
        }
        return responseResult;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 添加新的套餐，在examine_package表中添加套餐信息，在item_package表中添加项目套餐关联记录
     * @Date: 2020/1/16 21:09
     * @Param: [examinePackage, itemsId]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @Override
    public ResponseResult addExaminePackage(ExaminePackage examinePackage, String labItems, String radItems, String supItems, String speItems, String medItems, String cliItems) {
        ResponseResult responseResult = new ResponseResult();
        //检查该套餐名是否已经存在
        ExaminePackage examinePackage1 = examinePackageMapper.selPackageByName(examinePackage.getPackageName(), examinePackage.getUnitId());
        //如果该套餐名已经存在，返回建立失败信息
        System.out.println("根据套餐名获取的examinePackage:" + examinePackage);
        if (examinePackage1 != null) {
            responseResult.setCode(0);
            responseResult.setMsg("套餐建立失败，该套餐名已经存在！");
            return responseResult;
        }
        //添加套餐信息，
        int addPackageResult = examinePackageMapper.insExaminePackage(examinePackage);
        if (addPackageResult == 1) {
            //如果套餐添加成功，获取已经建立了的套餐（主要是为了获取新建套餐的id）
            ExaminePackage addedExaminePackage = examinePackageMapper.selPackageByName(examinePackage.getPackageName(), examinePackage.getUnitId());
            int examinePackageId = addedExaminePackage.getExaminePackageId();
            long unitId = examinePackage.getUnitId();
            boolean addItemPackageFailed = false;

            if (!labItems.equals("")) {
                String[] labItemIds = labItems.split(",");
                System.out.println(labItemIds);
                for (int i = 0; i < labItemIds.length; i++) {
                    int result = itemPackageMapper.insItemPackage(Integer.valueOf(labItemIds[i]), examinePackageId, unitId, "lab");
                    if (result != 1) {
                        addItemPackageFailed = true;
                        break;
                    }
                }
                if (addItemPackageFailed) {
                    responseResult.setCode(0);
                    responseResult.setMsg("套餐建立失败！");
                    return responseResult;
                }
            }


            if (!radItems.equals("")) {
                String[] radItemIds = radItems.split(",");
                System.out.println(radItemIds);
                for (int i = 0; i < radItemIds.length; i++) {
                    int result = itemPackageMapper.insItemPackage(Integer.valueOf(radItemIds[i]), examinePackageId, unitId, "rad");
                    if (result != 1) {
                        addItemPackageFailed = true;
                        break;
                    }
                }
                if (addItemPackageFailed) {
                    responseResult.setCode(0);
                    responseResult.setMsg("套餐建立失败！");
                    return responseResult;
                }
            }

            if (!supItems.equals("")) {
                String[] supItemIds = supItems.split(",");
                System.out.println(supItemIds);
                for (int i = 0; i < supItemIds.length; i++) {
                    int result = itemPackageMapper.insItemPackage(Integer.valueOf(supItemIds[i]), examinePackageId, unitId, "sup");
                    if (result != 1) {
                        addItemPackageFailed = true;
                        break;
                    }
                }
                if (addItemPackageFailed) {
                    responseResult.setCode(0);
                    responseResult.setMsg("套餐建立失败！");
                    return responseResult;
                }
            }

            if (!speItems.equals("")){
                String[] speItemIds = speItems.split(",");
                System.out.println(speItemIds);
                for (int i = 0; i < speItemIds.length; i++) {
                    int result = itemPackageMapper.insItemPackage(Integer.valueOf(speItemIds[i]), examinePackageId, unitId, "spe");
                    if (result != 1) {
                        addItemPackageFailed = true;
                        break;
                    }
                }
                if (addItemPackageFailed) {
                    responseResult.setCode(0);
                    responseResult.setMsg("套餐建立失败！");
                    return responseResult;
                }
            }

            if(!medItems.equals("")){
                String[] medItemIds = medItems.split(",");
                System.out.println(medItemIds);
                for (int i = 0; i < medItemIds.length; i++) {
                    int result = itemPackageMapper.insItemPackage(Integer.valueOf(medItemIds[i]), examinePackageId, unitId, "med");
                    if (result != 1) {
                        addItemPackageFailed = true;
                        break;
                    }
                }
                if (addItemPackageFailed) {
                    responseResult.setCode(0);
                    responseResult.setMsg("套餐建立失败！");
                    return responseResult;
                }
            }

            if(!cliItems.equals("")){
                String[] cliItemIds = cliItems.split(",");
                System.out.println(cliItemIds);
                for (int i = 0; i < cliItemIds.length; i++) {
                    int result = itemPackageMapper.insItemPackage(Integer.valueOf(cliItemIds[i]), examinePackageId, unitId, "cli");
                    if (result != 1) {
                        addItemPackageFailed = true;
                        break;
                    }
                }
                if (addItemPackageFailed) {
                    responseResult.setCode(0);
                    responseResult.setMsg("套餐建立失败！");
                    return responseResult;
                }
            }

        }
        responseResult.setCode(1);
        responseResult.setMsg("套餐建立成功！");
        return responseResult;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 获取适用于该性别的套餐列表
     * @Date: 2020/2/26 21:01
     * @Param: [examinePackage]
     * @Return: java.util.List<cn.edu.lzu.entity.ExaminePackage>
     **/
    @Override
    public List<ExaminePackage> selectUnitPackageListBySex(ExaminePackage examinePackage) {
        List<ExaminePackage> examinePackageList = examinePackageMapper.selUnitPackageBySex(examinePackage);
        return examinePackageList;
    }

    @Override
    public List<ExaminePackage> getUnitPackages(Long unitId) {
        List<ExaminePackage> packageList=examinePackageMapper.selUnitAllPackages(unitId);
        return packageList;
    }
}
